package com.lwx.chapter8;

import java.util.Random;

public class Main {
    private static double testHeapify(Integer[] testData, boolean isHeapify){
        long startTime = System.nanoTime();
        int n = testData.length;
        MaxHeap<Integer> maxHeap;
        if(isHeapify){
            maxHeap = new MaxHeap<>(testData);
        }else {
            maxHeap = new MaxHeap<>();
            for (int i = 0; i < n; i++) {
                maxHeap.add(testData[i]);
            }
        }
        long endTime = System.nanoTime();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = maxHeap.extractMax();
        }
        for (int i = 1; i < n; i++) {
            if(arr[i-1] < arr[i]){
                throw new IllegalArgumentException("Error");
            }
        }
        return (endTime - startTime) / 1000000000.0;
    }
    public static void main(String[] args) {
        int n = 1000000;
        Random random = new Random();
        Integer[] testData = new Integer[n];
        for (int i = 0; i < n; i++) {
            testData[i] = random.nextInt(Integer.MAX_VALUE);
        }
        //好像出了一点问题
        double t1 = testHeapify(testData, true);
        System.out.println("isHeapify cost time "+ t1);
        double t2 = testHeapify(testData, false);
        System.out.println("isNotHeapify cost time "+ t2);
        System.out.println("The gap is "+ (t2-t1));
//        System.out.println("Test MaxHeap Completed!");
    }
}
